package com.ymttest.database.sqlwapper.sqdistributor;

import java.util.HashMap;
import java.util.List;

import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.sqdistributor.DistributorApply;

public class DistributorApplyWrapper {
	
	SqlManager manager;

	public DistributorApplyWrapper() {
		manager = new SqlManager("sqdistributor");
	}

	@SuppressWarnings("unchecked")
	public DistributorApply selectByPrimaryKey(Long id) {

		DistributorApply distributorApply = null;
		List<DistributorApply> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorApplyMapper.selectByPrimaryKey",
						id);
		manager.commit();

		if (result.size() > 0) {
			distributorApply = result.get(0);
		}

		return distributorApply;
	}
	
	@SuppressWarnings("unchecked")
	public List<DistributorApply> selectByMerchantId(Long merchantId) {

		List<DistributorApply> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorApplyMapper.selectByMerchantId",
						merchantId);
		manager.commit();
		
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<DistributorApply> selectByMerchantIdAndReferrerId(Long merchantId, Long referrerId) {
		
		HashMap<String, Long> map = new HashMap<>();
		
		map.put("merchantId", merchantId);
		map.put("referrerId", referrerId);
		
		List<DistributorApply> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorApplyMapper.selectByMerchantIdAndReferrerId",
						map);
		manager.commit();

		if (result.size() == 0) {
			return null;
		} else {
			return result;
		}
	}
	
	@SuppressWarnings("unchecked")
	public DistributorApply selectByApplyUserIdAndReferrerId(Long applyUserId, Long referrerId) {
		
		DistributorApply distributorApply = null;
		
		HashMap<String, Long> map = new HashMap<>();
		
		map.put("applyUserId", applyUserId);
		map.put("referrerId", referrerId);
		
		List<DistributorApply> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorApplyMapper.selectByApplyUserIdAndReferrerId",
						map);
		manager.commit();

		if (result.size() > 0) {
			distributorApply = result.get(0);
		}

		return distributorApply;
	}
	
	public void updateAuditStatusToFalse(Long applyUserId, Long referrerId) {
		
		HashMap<String, Long> map = new HashMap<>();
		
		map.put("applyUserId", applyUserId);
		map.put("referrerId", referrerId);
		
		int result = manager
				.update(
						"com.ymttest.database.mapping.sqdistributor.DistributorApplyMapper.updateAuditStatusToFalse",
						map);
		manager.commit();

		if (result == 0) {
			Logger.comment("更新失败");
		}
	}
	
	public static void main(String[] args) {
		
		DistributorApplyWrapper test = new DistributorApplyWrapper();
		
		System.out.println(test.selectByApplyUserIdAndReferrerId(9295755L,
				500146001L).getApply_user_name());
	}
}
